<?php

namespace App\Models;

use App\Events\QiniuFopEvent;
use App\Helpers\FileTool;
use App\User;
use Illuminate\Database\Eloquent\Model;

class Material extends Model
{
    protected $table = 'material';

    protected $guarded = [];
    protected $casts = [
        'info' => 'array',
        'avinfo' => 'array',
    ];
    const ARTICLE = 'article';
    const IMAGE = 'image';
    const AUDIO = 'audio';
    const VIDEO = 'video';

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public static function fopFail($id)
    {
        $material = self::query()->where('persistent_id', $id)->first();
        if (empty($material)) {
            return true;
        }
        $material->fop_state = 2;
        $material->save();
        event(new QiniuFopEvent($id, [
            'state' => false
        ]));
    }

    public static function fopOk($id, $key)
    {
        $material = self::query()->where('persistent_id', $id)->first();
        if (empty($material)) {
            return true;
        }
        if ($material->type == Material::VIDEO) {
            $avinfo_json = file_get_contents(FileTool::video($key, 'avinfo'));
        } else {
            $avinfo_json = file_get_contents(FileTool::thumb($key, 'avinfo'));
        }


        $avinfo = json_decode($avinfo_json, true);

        $material->fop_state = 1;
        $material->key = $key;
        $material->avinfo = $avinfo;
        $material->save();
        switch ($material->type) {
            case self::AUDIO:
                event(new QiniuFopEvent($id, [
                    'state' => true,
                    'title' => $material->info['original_name'],
                    'author' => $material->user->liveroom->title,
                    'url' => FileTool::thumb($key),
                    'pic' => 'http://p1wp24hqw.bkt.clouddn.com/0593eda9c42c8b991be3f5f2abad58f5.png?imageView2/1/w/200/h/200/q/100',
                    'lrc' => '[00:00.00]lrc here\n[00:01.00]aplayer'
                ]));
                break;
            case self::VIDEO:
                event(new QiniuFopEvent($id, self::getVideo($material)));
                break;
        }

    }


    public static function del($material)
    {
        if (in_array($material->type, [self::IMAGE, self::AUDIO])) {
            \Storage::delete($material->data);
            if (!empty($material->key)) {
                \Storage::delete($material->key);
            }

        }
        if (in_array($material->type, [self::VIDEO])) {
            \Storage::disk('qiniu_video')->delete($material->data);
            if (!empty($material->key)) {
                \Storage::disk('qiniu_video')->delete($material->key);
            }

        }
        return $material->delete();

    }

    public static function getVideo($material)
    {
        return [
            'state' => true,
            'quality' => [
                [
                    'name' => '高清',
                    'url' => FileTool::video($material->key/*, 'avvod/m3u8/s/1280x720/vb/1000k'*/),
                    //'type' => 'hls'
                ],
                /*[
                    'name' => '标清',
                    'url' => FileTool::video($material->key, 'avvod/m3u8/s/854x480/vb/1000k'),
                    //'type' => 'hls'
                ]*/
            ],
            'defaultQuality' => 0,
            'pic' => FileTool::video($material->key, 'vframe/jpg/offset/1'),
            'thumbnails' => FileTool::video($material->key, 'vframe/jpg/offset/1')
        ];
    }
}
